<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/rest.js"></script>

<body>
    <form action="" onsubmit="return false" autocomplete="off">
        <div class="input-group">
            <labal>账&emsp;号:</labal>
            <input type="text" class="user" name="user">
            <span class="user_tips"></span>
        </div>
        <div class="input-group">
            <labal>密&emsp;码:</labal>
            <input type="password" class="pwd" name="pwd">
            <span class="pwd_tips"></span>
        </div>
        <div class="input-group">
            <input type="submit" class="submit" value="登录">
            <input type="reset" class="reset" value="取消">
        </div>
    </form>
</body>


<script>
    var user = document.getElementsByClassName('user')[0]
    var pwd = document.getElementsByClassName('pwd')[0]
    var submit = document.getElementsByClassName('submit')[0]


    submit.onclick = function () {
        var userVal = user.value
        var pwdVal = pwd.value
        if (userVal && pwdVal) {
            //用户名密码
            var xhr = new XMLHttpRequest()
            xhr.open('post', 'http://43.138.81.225/demo/php/login_account.php')
            xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded")
            xhr.send(`account=${userVal}&pwd=${pwdVal}`)
            xhr.onreadystatechange = function () {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    var res = xhr.responseText
                    var data = JSON.parse(res)
                    console.log(data)
                    // alert(data.status ? data.message + '用户名:' + data.user : '用户名或密码错误哦')
                    if (data.status) {
                        setCookie('userName', data.user)
                        // location.href = './index.html'
                        // location.href='./searchGoods.html'
                        var url = urlParamsParse();
                        var { ReturnUrl } = url;
                        if (ReturnUrl) { //其他页面条过来
                            location.href = ReturnUrl;
                        } else {
                            location.href = "./index.html";
                        }
                    }






                    else {
                        alert('用户名或密码错误哦')
                    }
                }
            }
        }
    }


    // submit.onclick = function () {
    //     var userName = user.value
    //     var password = pwd.value

    //     if (getCookie(userName)) {
    //         var userpwd = JSON.parse(getCookie(userName))[0].pwd
    //         if (password == userpwd) {
    //             location.href = '../购物车/html/01 购物车.html'
    //         } else {
    //             this.nextElementSibling.nextElementSibling.innerHTML = '请检查密码是否正确'
    //         }
    //     } else {
    //         this.nextElementSibling.nextElementSibling.innerHTML = '用户名未注册'
    //     }
    // }

</script>

</html>